import { getJobGetknowledgeoptions, postJobJobstep3Id } from "@/api/siderBar/job"
import type { RouteParams } from "../types"
import { EnumHrChatType, EnumHrFailedOperation, EnumHrIntentionMatchType, EnumHrMajorMatchType, EnumHrMatchType, EnumHrSuccessfulOperation, EnumHrUnmetAction, type JobDetailResult, type JobEditStep3Request } from "@/api/siderBar/model"
import { message } from "ant-design-vue"

interface FormState extends JobEditStep3Request {
}
interface UseStepThreeOptions {
    query: Ref<RouteParams>
    next: IFn
}
export function useStepThree({query, next}: UseStepThreeOptions){
    const chatTypeOptions: IOption<EnumHrChatType>[] = [
        { value: EnumHrChatType.智能分配, label: '智能分配' },
        { value: EnumHrChatType.平均分配, label: '平均分配' }
    ]
    const unmetActionOptions: IOption<EnumHrUnmetAction>[] = [
        { value: EnumHrUnmetAction.人工处理, label: '通知人工处理' },
        { value: EnumHrUnmetAction.跳过问题, label: '不回复跳过问题' },
        { value: EnumHrUnmetAction.默认回复, label: '使用默认回复' }
    ]
    const coditionMatchTypeOptions: IOption<EnumHrMatchType>[] = [
        { value: EnumHrMatchType.完全匹配, label: '完全匹配' },
        { value: EnumHrMatchType.宽松匹配, label: '宽松匹配（当经历+技能高匹配时放宽条件）' }
    ]
    const majorMatchTypeOptions: IOption<EnumHrMajorMatchType>[] = [
        { value: EnumHrMajorMatchType.严格匹配, label: '严格匹配（匹配度大于70%）' },
        { value: EnumHrMajorMatchType.宽松匹配, label: '宽松匹配（匹配度大于30%）' }
    ]
    const intentionMatchTypeOptions: IOption<EnumHrIntentionMatchType>[] = [
        { value: EnumHrIntentionMatchType.严格匹配, label: '严格匹配（求职者必须高意向）' },
        { value: EnumHrIntentionMatchType.宽松匹配, label: '宽松匹配（求职者犹豫也可以）' }
    ]
    const successfulOperationOptions: IOption<EnumHrSuccessfulOperation>[] = [
        { value: EnumHrSuccessfulOperation.求简历, label: '求简历' },
        { value: EnumHrSuccessfulOperation.换手机, label: '换手机' },
        { value: EnumHrSuccessfulOperation.换微信, label: '换微信' },
        { value: EnumHrSuccessfulOperation.约面试, label: '约面试' },
        { value: EnumHrSuccessfulOperation.人工处理, label: '通知人工处理' }
    ]
    const failedOperationOptions: IOption<EnumHrFailedOperation>[] = [
        { value: EnumHrFailedOperation.标记不合适, label: '标记不合适' },
        { value: EnumHrFailedOperation.发送婉拒话术, label: '发送婉拒话术' }
    ]
    const submitting = ref(false)
    const formState = ref(getInitialState())
    const showFailedReply = computed(() => formState.value.failedOperation && formState.value.failedOperation.includes(EnumHrFailedOperation.发送婉拒话术))
    const showUnmetReply = computed(() => formState.value.unmetAction === EnumHrUnmetAction.默认回复)
    const { data: knowledgeOptions, loading: knowledgeOptionsLoading } = useRequest(getJobGetknowledgeoptions)
    function getInitialState(): FormState {
        return {
            chatContent: "",
            chatType: EnumHrChatType.智能分配,
            coditionMatchType: EnumHrMatchType.宽松匹配,
            intentionMatchType: EnumHrIntentionMatchType.严格匹配,
            majorMatchType: EnumHrMajorMatchType.严格匹配,
            unmetAction: EnumHrUnmetAction.人工处理,
            welcomeContent: ""
        }
    }
    async function save() {
        if (!validate()) return
        try {
            submitting.value = true
            await postJobJobstep3Id(Number(query.value.id || 0), toValue(formState))
            submitting.value = false
            message.success('保存成功')
            next()
        } catch (error) {
            console.log(error)
            submitting.value = false
        }
    }
    function validate() {
        const state = toValue(formState)
        if (!isSetValue(state.chatType)) {
            message.error('请选择打招呼时段')
            return false
        }
        if (!state.chatContent) {
            message.error('请输入打招呼话术')
            return false
        }
        if (!state.welcomeContent) {
            message.error('请输入欢迎语配置')
            return false
        }
        if (showFailedReply.value && !state.failedReply) {
            message.error('请输入婉拒话术')
            return false
        }
        return true
    }
    function populateDetail(detail:JobDetailResult) {
        if (!detail || !detail.setting) return 
        const setting = detail.setting
        ;(<const>["chatType", "chatContent", "welcomeContent", "knowledgeId", "unmetAction", "unmetReply", "coditionMatchType", "majorMatchType", "intentionMatchType", "failedReply"])
        .forEach(field => {
            if (isSetValue(setting[field])) {
                // @ts-ignore
                formState.value[field] = setting[field]
            }
        })
        if (setting.successfulOperation) formState.value.successfulOperation = setting.successfulOperation
        if (setting.failedOperation) formState.value.failedOperation = setting.failedOperation
    }
    return {
        chatTypeOptions,
        unmetActionOptions,
        coditionMatchTypeOptions,
        majorMatchTypeOptions,
        intentionMatchTypeOptions,
        successfulOperationOptions,
        failedOperationOptions,
        formState,
        showFailedReply,
        showUnmetReply,
        submitting,
        save,
        populateDetail,
        knowledgeOptions,
        knowledgeOptionsLoading
    }
}